var i = 0;
var j = 0;
var comp = '';
var comp_sum = 0;
var comp_collection = '';
var comp_phase_collection = '';
var spec_sum = 0;
var spec_collection = '';
var spec_id_collection = '';
var spec_desc_collection = '';
var radio_items_id = '';
var radio_items_desc = '';
var show_page = '';
var show_result = '';
var mail_to = '';
var mail_subject = '';

/* Function */
function loadXmlFile() {
	var xml_http;
	var xml_Doc;
	var txt;
	// code for IE7+, Firefox, Chrome, Opera, Safari
	if (window.XMLHttpRequest) {
		xml_http=new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		xml_http=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xml_http.onreadystatechange=function() {
		if (xml_http.readyState == 4 && xml_http.status == 200) {
			xml_Doc=xml_http.responseXML;
			txt="";
			comp = xml_Doc.getElementsByTagName("component");
			comp_sum = comp.length;
			comp_collection = new Array(comp_sum);
			comp_phase_collection = new Array(comp_sum);
			spec_collection = new Array(comp_sum);
			spec_id_collection = new Array(comp_sum);
			spec_desc_collection = new Array(comp_sum);
			/* Parsing XML */
			parsingXML();
			/* initiate conversion table */
			init_conversion_tbl();
			/* Show Page */
			//showPage(2);
			$("#ShowBoardIdPage").hide();
			$("#CalculateBoardId").hide();
			$("#selectMenu").show();
		}
	}
	xml_http.open("GET","xml/AMLogic-m3-product.xml",true);
	xml_http.send();
}

function parsingXML() {
	for(i = 0; i < comp_sum; i++) {
	    comp_collection[i] = comp[i].getAttribute("name");
	    comp_phase_collection[i] = comp[i].getAttribute("phase");
	    spec_sum = comp[i].getElementsByTagName("component-item").length;
	    spec_collection[i] = new Array(spec_sum);
	    spec_id_collection[i] = new Array(spec_sum);
	    spec_desc_collection[i] = new Array(spec_sum);
	    for(j = 0; j < spec_sum; j++) {
			//spec_collection[i][j] = comp[i].children[j].getAttribute("name");
			//spec_id_collection[i][j] = comp[i].children[j].getAttribute("value");
			//spec_desc_collection[i][j] = comp[i].children[j].getElementsByTagName("text")[0].childNodes[0].nodeValue;
			spec_collection[i][j] = $(comp[i]).children(':eq('+j+')').attr('name');
			spec_id_collection[i][j] = $(comp[i]).children(':eq('+j+')').attr('value');
			spec_desc_collection[i][j] = $(comp[i]).children(':eq('+j+')').children('text').text();
	    }
	}
}
var component2choose = [];
// phase:
// 0: show all
// 1: only M1
// 2: only M2
function showPage(phase) {
    init_array(component2choose);
    mail_to = document.getElementById("Mail");
    mail_to.value = "";
    radio_items_id = new Array;
    radio_items_desc = new Array;
    show_page = document.getElementById("ShowPage");
    for(i = 0; i < comp_sum; i++) {
	if ((phase == 0) || (comp_phase_collection[i] == "M1+M2") ||
	    ((phase == 1) && (comp_phase_collection[i] == "M1")) ||
	    ((phase == 2) && (comp_phase_collection[i] == "M2")))
	    {
		var title = "Choose " + comp_collection[i] + " : ";
		if(i == 0) { show_page.innerHTML += title + "<br />"; }
		else { show_page.innerHTML +="<br />" + title + "<br />"; }
		for(j = 0; j < spec_collection[i].length; j++) {
		    var item = '<input type="radio" name="' + comp_collection[i] + '" value="' + spec_id_collection[i][j] + '">[' + spec_id_collection[i][j] + '] ' + spec_desc_collection[i][j] + '</input><br />';
		    show_page.innerHTML += item;
		    radio_items_id.push(spec_id_collection[i][j]);
		    radio_items_desc.push(spec_desc_collection[i][j]);
		}
		component2choose[comp_collection[i]] = 1;
	}
    }
    cleanColumn();
}

function do_M1_Phase()
{
    /* Show Page */
    $("#selectMenu").hide();
    $("#ShowBoardIdPage").show();
    showPage(0);
}
function do_M2_Phase()
{
    /* Show Page */
    $("#selectMenu").hide();
    $("#ShowBoardIdPage").show();
    showPage(2);
}

